# Maintainer ncihnegn <ningchen@outlook.com>
# Maintainer: Alethea Rose <alethea@alethearose.com>

_realname=ghc
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=8.0.2
pkgrel=1
pkgdesc='The Glorious Glasgow Haskell Compilation System'
arch=('any')
group="$MINGW_PACKAGE_PREFIX-haskell-platform"
url='https://www.haskell.org/ghc/'
license=('The Glasgow Haskell Compiler License')
depends=("${MINGW_PACKAGE_PREFIX}-binutils"
         "${MINGW_PACKAGE_PREFIX}-crt-git"
         "${MINGW_PACKAGE_PREFIX}-gcc"
         "${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-gmp"
         "${MINGW_PACKAGE_PREFIX}-headers"
         "${MINGW_PACKAGE_PREFIX}-isl"
         "${MINGW_PACKAGE_PREFIX}-libiconv"
         "${MINGW_PACKAGE_PREFIX}-libffi"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread"
         "${MINGW_PACKAGE_PREFIX}-mpc"
	 "${MINGW_PACKAGE_PREFIX}-mpfr")
provides=("${MINGW_PACKAGE_PREFIX}-haskell-array=0.5.1.1"
          "${MINGW_PACKAGE_PREFIX}-haskell-base=4.9.1.0"
          "${MINGW_PACKAGE_PREFIX}-haskell-binary=0.8.3.0"
          "${MINGW_PACKAGE_PREFIX}-haskell-bytestring=0.10.8.1"
          "${MINGW_PACKAGE_PREFIX}-haskell-containers=0.5.7.1"
          "${MINGW_PACKAGE_PREFIX}-haskell-deepseq=1.4.2.0"
          "${MINGW_PACKAGE_PREFIX}-haskell-directory=1.3.0.0"
          "${MINGW_PACKAGE_PREFIX}-haskell-filepath=1.4.1.1"
          "${MINGW_PACKAGE_PREFIX}-haskell-ghc-boot=8.0.2"
          "${MINGW_PACKAGE_PREFIX}-haskell-ghc-boot-th=8.0.2"
          "${MINGW_PACKAGE_PREFIX}-haskell-ghci=8.0.2"
          "${MINGW_PACKAGE_PREFIX}-haskell-ghc-prim=0.5.0.0"
          "${MINGW_PACKAGE_PREFIX}-haskell-haskeline=0.7.3.0"
          "${MINGW_PACKAGE_PREFIX}-haskell-hoopl=3.10.2.1"
          "${MINGW_PACKAGE_PREFIX}-haskell-hpc=0.6.0.3"
          "${MINGW_PACKAGE_PREFIX}-haskell-integer-gmp=1.0.0.1"
          "${MINGW_PACKAGE_PREFIX}-haskell-pretty=1.1.3.3"
          "${MINGW_PACKAGE_PREFIX}-haskell-process=1.4.3.0"
          "${MINGW_PACKAGE_PREFIX}-haskell-template-haskell=2.11.1.0"
          "${MINGW_PACKAGE_PREFIX}-haskell-terminfo=0.4.0.2"
          "${MINGW_PACKAGE_PREFIX}-haskell-time=1.6.0.1"
          "${MINGW_PACKAGE_PREFIX}-haskell-transformers=0.5.2.0"
          "${MINGW_PACKAGE_PREFIX}-haskell-win32=2.3.1.1"
          "${MINGW_PACKAGE_PREFIX}-haskell-xhtml=3000.2.1"
          "${MINGW_PACKAGE_PREFIX}-haskell-cabal=1.24.2.0")
replaces=("${MINGW_PACKAGE_PREFIX}-haskell-array"
          "${MINGW_PACKAGE_PREFIX}-haskell-base"
          "${MINGW_PACKAGE_PREFIX}-haskell-binary"
          "${MINGW_PACKAGE_PREFIX}-haskell-bytestring"
          "${MINGW_PACKAGE_PREFIX}-haskell-containers"
          "${MINGW_PACKAGE_PREFIX}-haskell-deepseq"
          "${MINGW_PACKAGE_PREFIX}-haskell-directory"
          "${MINGW_PACKAGE_PREFIX}-haskell-filepath"
          "${MINGW_PACKAGE_PREFIX}-haskell-ghc-boot"
          "${MINGW_PACKAGE_PREFIX}-haskell-ghc-boot-th"
          "${MINGW_PACKAGE_PREFIX}-haskell-ghci"
          "${MINGW_PACKAGE_PREFIX}-haskell-ghc-prim"
          "${MINGW_PACKAGE_PREFIX}-haskell-haskeline"
          "${MINGW_PACKAGE_PREFIX}-haskell-hoopl"
          "${MINGW_PACKAGE_PREFIX}-haskell-hpc"
          "${MINGW_PACKAGE_PREFIX}-haskell-integer-gmp"
          "${MINGW_PACKAGE_PREFIX}-haskell-pretty"
          "${MINGW_PACKAGE_PREFIX}-haskell-process"
          "${MINGW_PACKAGE_PREFIX}-haskell-template-haskell"
          "${MINGW_PACKAGE_PREFIX}-haskell-terminfo"
          "${MINGW_PACKAGE_PREFIX}-haskell-time"
          "${MINGW_PACKAGE_PREFIX}-haskell-transformers"
          "${MINGW_PACKAGE_PREFIX}-haskell-win32"
          "${MINGW_PACKAGE_PREFIX}-haskell-xhtml"
          "${MINGW_PACKAGE_PREFIX}-haskell-cabal")


makedepends=("${MINGW_PACKAGE_PREFIX}-python3-sphinx"
             "${MINGW_PACKAGE_PREFIX}-llvm"
	     'make'
	     'perl')
optdepends=("${MINGW_PACKAGE_PREFIX}-llvm: use LLVM for an optimized backend")

install="${MINGW_PACKAGE_PREFIX}-ghc.install"

_hsarch=$CARCH
if [[ $_hsarch == "i686" ]]; then
  _hsarch="i386"
fi
_hooks=('ghc-register.hook' 'ghc-unregister.hook' 'ghc-rebuild-doc-index.hook')

source=(
  "https://downloads.haskell.org/~ghc/$pkgver/ghc-$pkgver-src.tar.xz"{,.sig}
  "https://downloads.haskell.org/~ghc/$pkgver/ghc-$pkgver-testsuite.tar.xz"{,.sig}
  "https://downloads.haskell.org/~ghc/$pkgver/ghc-$pkgver-$_hsarch-unknown-mingw32.tar.xz"{,.sig}
  'build.mk'
  '0001-SysTools-Revert-linker-flags-change.patch'
  '0002-Switch-to-LLVM-version-3.9.patch'
  '0003-Use-system-build-tools.patch'
  '0004-Add-MinGW-_lock_file-symbol.patch'
  '0005-Add-MinGW-_unlock_file-symbol.patch'
  'ghc-register.hook'
  'ghc-unregister.hook'
  'ghc-rebuild-doc-index.hook'
)
noextract=("ghc-$pkgver-$_hsarch-unknown-mingw32.tar.xz")

sha256sums=('11625453e1d0686b3fa6739988f70ecac836cadc30b9f0c8b49ef9091d6118b1'
            'SKIP'
            '52235d299eb56292f2c273dc490792788b8ba11f4dc600035d050c8a4c1f4cf2'
            'SKIP'
            'SKIP'
            'SKIP'
            'c4284efe02de55fbd3dedadb785ad977d123a7e257ef3d77d33ee0a127e9248d'
            'bf8e495a657eaacf8adfa0fb23a52f578a365e0a0f3559bef14b6ca58acb9eda'
            '555dad54247f4c8b58f5b915c66f4f947341b3320a280ec08894ab812946f63b'
            'f7d6c844b68be2dfcb89583c8315fe1eac97b3dca805a9021a5d52352e4fb073'
            '779174f48caa342bc2515c157343dffd2b22979a65c2ae0f73a6dc30948d84af'
            'c0926878e278717431a02ede49cea628d1d80a83459a0a99744d9e2cdbf1ece6'
            '633b6a02efa3a6f30c4c40062f24bf0ffa9689c21d0e16da09ed8b699ba6050e'
            '33bde1f07e6317017e9c7fc7b2e7ad2d63ed3dcde407a23401d0a03861301cea'
            '31f3b191bcfe7ad09a886e90f6d71c3be5bee02304a81bffe61ac1f5dcdae703')

validpgpkeys=('FFEB7CE81E16A36B3E2DED6F2DE04D4E97DB64AD') # Benjamin Gamari <ben@well-typed.com>

prepare() {
  cd "$srcdir"

  mkdir -p $CARCH/host
  rm -rf $CARCH/ghc-$pkgver
  mv {,$CARCH/}ghc-$pkgver

  msg "Extracting binaries: ghc-$pkgver-$_hsarch-unknown-mingw32.tar.xz..."
  bsdtar -xf ghc-$pkgver-$_hsarch-unknown-mingw32.tar.xz \
    -C $CARCH/host --strip-components 1

  for _hook in "${_hooks[@]}"; do
    echo $_hook
    sed -e "s|MINGW_PREFIX|$(basename $MINGW_PREFIX)|" \
      $_hook > $CARCH/$_hook
  done

  export PATH="$srcdir/$CARCH/host/bin:$PATH"

  cp -v "$srcdir/build.mk" "$srcdir/$CARCH/ghc-$pkgver/mk/build.mk"

  cd "$srcdir/$CARCH/ghc-$pkgver"
  patch -Np1 -i "$srcdir/0001-SysTools-Revert-linker-flags-change.patch"
  patch -Np1 -i "$srcdir/0002-Switch-to-LLVM-version-3.9.patch"
  patch -Np1 -i "$srcdir/0003-Use-system-build-tools.patch"
  patch -Np1 -i "$srcdir/0004-Add-MinGW-_lock_file-symbol.patch"
  patch -Np1 -i "$srcdir/0005-Add-MinGW-_unlock_file-symbol.patch"
}

build() {
  export PATH="$srcdir/$CARCH/host/bin:$PATH"
  cd "$srcdir/$CARCH/ghc-$pkgver"

  ./boot
  ./configure \
    --with-system-libffi \
    --with-ffi-includes="$(cygpath -u $(pkg-config --variable=includedir libffi))"

  make
  make html
}

check() {
  cd "$srcdir/$CARCH/ghc-$pkgver"

  make test
}

package() {
  cd "$srcdir/$CARCH/ghc-$pkgver"

  make prefix="${pkgdir}${MINGW_PREFIX}" \
    docdir="${pkgdir}${MINGW_PREFIX}/share/docs/ghc" \
    install

  for _hook in "${_hooks[@]}"; do
    install -Dm644 "$srcdir/$CARCH/$_hook" \
      "$pkgdir/usr/share/libalpm/hooks/$MINGW_PACKAGE_PREFIX-$_hook"
  done

  mkdir -vp "${pkgdir}${MINGW_PREFIX}/share/haskell/"{register,unregister}

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/ghc/LICENSE"
}
